<?php

require_once('../../connection/_dbconnect_.php');
require_once('m_activity_log.php');

function getMonthSchedules($month,$year,$usernames){
	
	// TO HOLD ALL USER MONTH SCHEDULES
	$user_scheduleArray = array();
	
	
	foreach ($usernames as $currUser){
		
		// To hold this user's month schedule
		$user_month_schedule = array();
		
		$sql = 	"SELECT  sr.iduser AS username,
						 sr.idcalendar_day AS calendar_day,
						 sr.iduser_schedule_status AS status_id,
						 ss.name AS status \n"
				. "FROM  schedule_registry sr\n"
				. "JOIN user_schedule_status ss ON (sr.iduser_schedule_status = ss.iduser_schedule_status)\n"
				. "WHERE  sr.iduser LIKE '".$currUser."'\n"
				. "AND sr.idcalendar_day >= '".$year."-".$month."-01'\n"
				. "AND sr.idcalendar_day < '".$year."-".((int) $month + 1)."-01'\n"
				. "ORDER BY sr.idcalendar_day ASC" ;
		
		$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
		
		if (!$result) 
			return false;
		else if(!(mysql_num_rows($result)==0)) {
			
			$current_userArray= array();
			
			while($obj = mysql_fetch_object($result)){
				
				$current_userArray[] = $obj;
			}
			
			$days_in_month = date('t',mktime(0,0,0,$month,1,$year));
			$user_month_schedule = array_fill(1,$days_in_month+1,"empty");
			
			//first slot in the array is the username
			$user_month_schedule[0] = $currUser;
			
			//print_r($current_userArray);exit;
			//will check each day status and assign it to 
			//the specific day number within $user_month_schedule
			
			for ($i=0; $i<sizeof($current_userArray);$i++){
				
				$dayEntry = explode('-',$current_userArray[$i]->calendar_day);
				
				$user_month_schedule[((int) $dayEntry[2])]= $current_userArray[$i]->status;
				
			}			
			
			//FALTA REVISAR CAMBIOS DE HORARIO
			
			$user_scheduleArray[] = $user_month_schedule;				
			
		} else {
			$days_in_month = date('t',mktime(0,0,0,$month,1,$year));
			$user_month_schedule = array_fill(1,$days_in_month+1,"empty");	
			$user_month_schedule[0] = $currUser;
			
			//FALTA REVISAR CAMBIOS DE HORARIO
			
			$user_scheduleArray[]= $user_month_schedule;
		}
	}
	
	//print_r($user_scheduleArray);exit;
	
	
	return $user_scheduleArray;
}


function getUserScheduleStatus($username,$year,$month,$day){
	
	$parentArray = array();
	
	
	$sql = 	"SELECT  sr.iduser AS username,
					 sr.idcalendar_day AS calendar_day,
					 sr.iduser_schedule_status AS status_id,
					 ss.name AS status \n"
			. "FROM  schedule_registry sr\n"
			. "JOIN user_schedule_status ss ON (sr.iduser_schedule_status = ss.iduser_schedule_status)\n"
			. "WHERE  sr.iduser LIKE '".$username."'\n"
			. "AND sr.idcalendar_day = '".$year."-".$month."-".$day."'\n ;";
	
	$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
	
	if (!$result) 
		return false;
	else if(mysql_num_rows($result)==0){
	
		$obj = array(
					 'username' => $username,
					 'calendar_day' => $year.'-'.$month.'-'.$day,
					 'status_id' => 0,
					 'status'=>'');
		
		return $obj;
	}
	else {
		$obj = mysql_fetch_object($result);
		
		$parentArray = array(
					 'username' => $obj->username,
					 'calendar_day' => $obj->calendar_day,
					 'status_id' => $obj->status_id,
					 'status'=> $obj->status);
		
		return $parentArray;
	}
}


function loadScheduleStatus(){
	
	$parentArray = array();
	
	$sql = "select iduser_schedule_status,name from user_schedule_status";
	
	$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
	
	if (!$result) 
		return false;
	else {

		$prefixArray = array();
		$prefixArray[]= 0;
		$prefixArray[]= '---';
		
		$parentArray[]=$prefixArray;
	
		while($obj = mysql_fetch_object($result)){
			$childArray = array();		
			$childArray[] = $obj->iduser_schedule_status;
			$childArray[] = $obj->name;
			
			$parentArray[]= $childArray;
		}
		return $parentArray;
	}
}

function saveScheduleUser($user,$calendarDay,$status){
	
	//check if an entry exists in schedule_registry
	$sql = 	"SELECT  sr.iduser AS username,
					 sr.idcalendar_day AS calendar_day \n"
			. "FROM  schedule_registry sr\n"
			. "WHERE  sr.iduser LIKE '".$user."'\n"
			. "AND sr.idcalendar_day = '".$calendarDay."';";
			
	$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
	
	if (!$result) 
		return false;		
	else if (mysql_num_rows($result)==0) {
	
		//Check if calendar day exists
		
		$sql = "select * from calendar_day where idcalendar_day = '".$calendarDay."';" ;
		
		$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
		
		if (!$result) 
			return false;		
		else if (mysql_num_rows($result)==0) {
		
			//create calendar day first
			$sql = "INSERT INTO `calendar_day` (`idcalendar_day`)
					VALUES (DATE( '".$calendarDay."'));";
					
			$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
			
			if (!$result) 
				return false;

		}
		
		//insert entry into schedule_registry table
		$sql = "INSERT INTO schedule_registry 
				VALUES (
					'".$calendarDay."',
					'".$user."',
					'".$status."',
					Null
				);";
		
		$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
		
		if (!$result) 
			return false;	
		else {
			$date =new DateTime('NOW');		
			$result=addLogEntry($date->format('Y-m-d'), "Schedule Registry for user=".$user." for ".$calendarDay." was added");
			return true;
		}	
	}
	else {
		//TESTED, WORKS!
		$sql = "UPDATE `schedule_registry` 
				SET `iduser_schedule_status` = '".$status."' 
				WHERE `idcalendar_day` = '".$calendarDay."' 
				AND `iduser` = '".$user."';";
		
		$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
		
		if (!$result) 
			return false;		
		else {
			$date =new DateTime('NOW');		
			$result=addLogEntry($date->format('Y-m-d'), "Schedule Registry for user=".$user." for ".$calendarDay." was modified");
			return true;
		}	
	}
}
	
function deleteScheduleUser($user,$calendarDay){
	
	$sql = "DELETE FROM `db_proyecto_progra4`.`schedule_registry` 
			WHERE `idcalendar_day` = '".$calendarDay."' 
			AND `iduser` = '".$user."'\n 
			LIMIT 1;";
	
	$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
	
	if (!$result) {
		return false;
	} else{
		$date =new DateTime('NOW');		
		$result=addLogEntry($date->format('Y-m-d'), "Schedule Registry for user=".$user." for ".$calendarDay." was deleted");
		return true;
	}
}



?>